package com.lanhao.aot.websocket;

import java.net.URI;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * websocket客户端监听类
 * 
 * @author 。
 */
public class MyWebSocketClient extends WebSocketClient {

	private static Logger logger = LoggerFactory.getLogger(MyWebSocketClient.class);

	public MyWebSocketClient(URI serverUri) {
		super(serverUri);
	}

	@Override
	public void onOpen(ServerHandshake serverHandshake) {
		logger.info(">>>>>>>>>>>websocket open");
//		try {
//			TimeUnit.SECONDS.sleep(5);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
//
////		HexUtil.decodeHex("CA20 0318020102AC");
//		// 0xCA 0x20 id cmd datalen data 0xAC
//		byte[] b = new byte[8];
//		b[0] = (byte) 0xCA;
//		b[1] = 0x20;
//		b[2] = 0x03;
//		b[3] = 0x18;
//		b[4] = 0x02;
//		b[5] = 0x01;
//		b[6] = 0x02;
//		b[7] = (byte) 0xAC;
//		
//		send(b);
	}

	@Override
	public void onMessage(String s) {
		logger.info(">>>>>>>>>> websocket message s =" + s);

	}

	@Override
	public void onClose(int i, String s, boolean b) {
		logger.info(">>>>>>>>>>>websocket close");
	}

	@Override
	public void onError(Exception e) {
		logger.error(">>>>>>>>>websocket error {}", e);
	}

}